/**
 * 限额新增
 */
angular.module('inspinia',['angularFileUpload']).controller('updateTransLimitRuleCtrl',function($scope,$http,i18nService,$state,FileUploader,$stateParams){
    i18nService.setCurrentLang('zh-cn');  //设置语言为中文

    $scope.limitActiones= [{text:"请选择业务类型",value:""},{text:"限制交易",value:"TRANS"},{text:"限制额度",value:"QUOTA"}];
    $scope.info={};

    $scope.getDatailInfo=function(){
        $http.post('transLimit/selectTransLimitRuleDetail',
            "id="+$stateParams.id,
            {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .success(function(data){
                if(data.status){
                    $scope.info=data.data;
                    $scope.info.cardType= Number($scope.info.cardType);
                    if($scope.info.teamId=="all"){
                        $scope.info.teamType=$scope.info.teamId;
                    }else{
                        $scope.info.teamType="1";
                        $scope.info.teamIdList=$scope.info.teamId.split(",");
                        $scope.getTeamList();
                    }
                    if($scope.info.rulesProvinces=="all"){
                        $scope.info.areaType=$scope.info.rulesProvinces;
                    }else{
                        $scope.info.areaType="1";
                        $scope.info.provincesList=$scope.info.rulesProvinces.split(",");
                        $scope.info.cityList=$scope.info.rulesCity.split(",");
                        $scope.getAreaTypeList();
                    }
                }
            })
    };
    $scope.getDatailInfo();

    $scope.getTeamList=function(){
        if($scope.info.teamType==1){
            $scope.teamGridApi.selection.clearSelectedRows();
            //组织
            $http.get('teamInfo/queryTeamName.do').success(function(msg){
                $scope.teamGrid.data = msg.teamInfo;
            });
        }
    }
    $scope.getTeamList();

    //商户省份表格
    $scope.teamGrid = {
        columnDefs: [
            {field: 'teamName',displayName: '所属组织',width: 200},
        ],
        onRegisterApi : function(gridApi) {
            $scope.teamGridApi = gridApi;
        },
        isRowSelectable: function(row){ // 选中行
            if($scope.info.teamIdList){
                for(var i=0;i<$scope.info.teamIdList.length;i++){
                    if(row.entity.teamId==$scope.info.teamIdList[i]){
                        row.grid.api.selection.selectRow(row.entity);
                        break;
                    }
                }
            }
        }
    };

    $scope.getAreaTypeList=function(){
        if($scope.info.areaType==1){
            $scope.provinceGridApi.selection.clearSelectedRows();
            $scope.cityGridApi.selection.clearSelectedRows();
            $scope.getAreaList(0, "p", function (data) {
                $scope.provinceGrid.data = data;
            });
        }
    }
    $scope.getAreaTypeList();

    //获取所有省份
    $scope.getAreaList=function(name,type,callback){
        if(name == null || name=="undefine"){
            return;
        }
        $http.post('areaInfo/getAreaByName.do','name='+name+'&&type='+type,
            {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
        ).success(function(data){
            callback(data);
        }).error(function(){
        });
    }

    //省份表格
    $scope.provinceGrid = {
        columnDefs: [
            {field: 'name',displayName: '省份',width: 200},
        ],
        onRegisterApi : function(gridApi) {
            $scope.provinceGridApi = gridApi;
            $scope.provinceGridApi.selection.on.rowSelectionChanged($scope,function(row,event){
                //行选中事件
                $scope.findCity();
            });
            $scope.provinceGridApi.selection.on.rowSelectionChangedBatch($scope,function(row,event){
                //全选事件
                $scope.findCity();
            });
        },
        isRowSelectable: function(row){ // 选中行
            if($scope.info.provincesList){
                for(var i=0;i<$scope.info.provincesList.length;i++){
                    if(row.entity.name==$scope.info.provincesList[i]){
                        row.grid.api.selection.selectRow(row.entity);
                        break;
                    }
                }
            }
        }
    };

    $scope.findCity=function(){
        $http.post("areaInfo/getAreaCityByParentId",angular.toJson({provinceList:$scope.provinceGridApi.selection.getSelectedRows()})).success(function(data){
            $scope.cityGrid.data = data;
        });
    }

    //市表格
    $scope.cityGrid = {
        columnDefs: [
            {field: 'name',displayName:'市',width: 200},
        ],
        onRegisterApi : function(gridApi) {
            $scope.cityGridApi = gridApi;
        },
        isRowSelectable: function(row){ // 选中行
            if($scope.info.cityList){
                for(var i=0;i<$scope.info.cityList.length;i++){
                    if(row.entity.name==$scope.info.cityList[i]){
                        row.grid.api.selection.selectRow(row.entity);
                        break;
                    }
                }
            }
        }
    };


    $scope.submitting = false;

    $scope.commitInfo=function () {
        if($scope.submitting){
            return;
        }
        if($scope.info.limitAction=='TRANS'){
            $scope.info.singleDayAmount=null;
            $scope.info.singleCountAmount=null;
            $scope.info.singleDaycardAmount=null;
            $scope.info.singleDaycardCount=null;
        }
        $scope.info.rulesProvinces="";
        $scope.info.rulesCity="";
        $scope.info.teamId="";
        if($scope.info.teamType==1){
            if($scope.teamGridApi.selection.getSelectedRows().length<=0){
                $scope.notice("指定组织不能为空");
                return;
            }
            angular.forEach($scope.teamGridApi.selection.getSelectedRows(), function(data,index){
                $scope.info.teamId=$scope.info.teamId+data.teamId+",";
            });
            $scope.info.teamId=$scope.info.teamId.substring(0,$scope.info.teamId.length-1);
        }else{
            $scope.info.teamId=$scope.info.teamType;
        }
        if($scope.info.areaType==1){
            if($scope.provinceGridApi.selection.getSelectedRows().length<=0||$scope.cityGridApi.selection.getSelectedRows().length<=0){
                $scope.notice("指定省市不能为空");
                return;
            }
            angular.forEach($scope.provinceGridApi.selection.getSelectedRows(), function(data,index){
                $scope.info.rulesProvinces=$scope.info.rulesProvinces+data.name+",";
            });
            $scope.info.rulesProvinces=$scope.info.rulesProvinces.substring(0,$scope.info.rulesProvinces.length-1);
            angular.forEach($scope.cityGridApi.selection.getSelectedRows(), function(data,index){
                $scope.info.rulesCity=$scope.info.rulesCity+data.name+",";
            });
            $scope.info.rulesCity=$scope.info.rulesCity.substring(0,$scope.info.rulesCity.length-1);
        }else{
            $scope.info.rulesProvinces=$scope.info.areaType;
            $scope.info.rulesCity=$scope.info.areaType;
        }
        $scope.submitting = true;
        $http.post("transLimit/updateTransLimitRule","info="+angular.toJson($scope.info),
            {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .success(function(data){
                $scope.submitting = false;
                if(data.status){
                    $scope.notice(data.msg);
                    $state.transitionTo('risk.transLimitRule',null,{reload:true});
                }else{
                    $scope.notice(data.msg);
                }
            })
            .error(function(data){
                $scope.submitting = false;
                $scope.notice(data.msg);
            });
    };
});